# --------------------------------------------------
# Russian Invasion of Ukraine and Chinese Public
# Support for War
#
# Authors: Deniz Aksoy, Ted Enamorado, Tony Yang
#
# The lines below reproduce the main Figures and 
# Tables reported in the paper
# --------------------------------------------------
rm(list=ls())

# Auxilliary Functions to create the plots:
source("00_Functions_All.R")

set.seed('123')

# Load the data
experiment1 <- fread("Experiment2022.csv")
experiment2 <- fread("Experiment2023.csv")

# Remove from experiment 1 the Lack of Military action
experiment1 <- experiment1[Group != 4, ]

## ------------------------------
## Figure 1
## ------------------------------
pdf(file = "./Figures_Paper/Figure_01.pdf", width = 16, height = 6)
main.result(data1 = experiment1, data2 = experiment2, plot.covs = TRUE, title.r = "Figure 1b", title.l = "Figure 1a")
dev.off()

## ------------------------------
## Figure 2
## ------------------------------

# Use the regression results to produce Figure 2
pdf(file = "./Figures_Paper/Figure_02.pdf", width = 16, height = 6)
main.result.inva(data1 = experiment1, data2 = experiment2, plot.covs = TRUE, title.l = "Figure 2a", title.r = "Figure 2b")
dev.off()

## ------------------------------
## Table 1
## ------------------------------

# Main Regressions:
# Outcome: Support for the Use of Force
main1_s1 <- lm(Military ~ as.factor(RussianGroup), data = experiment1)
main1_s2 <- lm(Military ~ as.factor(RussianGroup), data = experiment2)

# Outcome: Support for the Use of Force Against Taiwan
main2_s1 <- lm(Taiwan ~ as.factor(RussianGroup), data = experiment1)
main2_s2 <- lm(Taiwan ~ as.factor(RussianGroup), data = experiment2)

# Subset the data to exclude the control group
# Invasion Treatment becomes the baseline
data.sub1 <- experiment1[!(experiment1$Group %in% 0), ]    
data.sub2 <- experiment2[!(experiment2$Group %in% 0), ]    

# Regressions with Invasion Treatment as baseline:
# Outcome: Support for the Use of Force
main1_s1_inv <- lm(Military ~ as.factor(RussianGroup), data = data.sub1)
main1_s2_inv <- lm(Military ~ as.factor(RussianGroup), data = data.sub2)

# Outcome: Support for the Use of Force Against Taiwan
main2_s1_inv <- lm(Taiwan ~ as.factor(RussianGroup), data = data.sub1)
main2_s2_inv <- lm(Taiwan ~ as.factor(RussianGroup), data = data.sub2)

# Regressions for the Taiwan Questions used in Figure 3
# We need them to adjust the p-values 
main3a_s2 <- lm(Taiwan_Invasion ~ as.factor(RussianGroup), data = experiment2)
main3b_s2 <- lm(Taiwan_Coersion ~ as.factor(RussianGroup), data = experiment2)

adj.p.s1 <- p.adjust(c(summary(main1_s1)$coefficients[2:4,4], summary(main1_s1_inv)$coefficients[2:3,4], summary(main2_s1)$coefficients[2:4,4], summary(main2_s1_inv)$coefficients[2:3,4]), method = "fdr")
adj.p.s2 <- p.adjust(c(summary(main1_s2)$coefficients[2:4,4], summary(main1_s2_inv)$coefficients[2:3,4], summary(main2_s2)$coefficients[2:4,4], summary(main2_s2_inv)$coefficients[2:3,4], summary(main3a_s2)$coefficients[2:4,4], summary(main3b_s2)$coefficients[2:4,4]), method = "fdr")

# Table 1A
c1 <- round(c(coef(main1_s1)["as.factor(RussianGroup)Invasion"], coef(main2_s1)["as.factor(RussianGroup)Invasion"],
              coef(main1_s2)["as.factor(RussianGroup)Invasion"], coef(main2_s2)["as.factor(RussianGroup)Invasion"]), 2)
p1 <- round(c(summary(main1_s1)$coefficients["as.factor(RussianGroup)Invasion", 4], summary(main2_s1)$coefficients["as.factor(RussianGroup)Invasion", 4],
              summary(main1_s2)$coefficients["as.factor(RussianGroup)Invasion", 4], summary(main2_s2)$coefficients["as.factor(RussianGroup)Invasion", 4]), 4)
a1 <- round(c(adj.p.s1[names(adj.p.s1) %in% "as.factor(RussianGroup)Invasion"][1], adj.p.s1[names(adj.p.s1) %in% "as.factor(RussianGroup)Invasion"][2],
              adj.p.s2[names(adj.p.s2) %in% "as.factor(RussianGroup)Invasion"][1], adj.p.s2[names(adj.p.s2) %in% "as.factor(RussianGroup)Invasion"][2]), 4)     
    
c2 <- round(c(coef(main1_s1)["as.factor(RussianGroup)Sanction"], coef(main2_s1)["as.factor(RussianGroup)Sanction"],
        coef(main1_s2)["as.factor(RussianGroup)Sanction"], coef(main2_s2)["as.factor(RussianGroup)Sanction"]), 2)
p2 <- round(c(summary(main1_s1)$coefficients["as.factor(RussianGroup)Sanction", 4], summary(main2_s1)$coefficients["as.factor(RussianGroup)Sanction", 4],
              summary(main1_s2)$coefficients["as.factor(RussianGroup)Sanction", 4], summary(main2_s2)$coefficients["as.factor(RussianGroup)Sanction", 4]), 4)
a2 <- round(c(adj.p.s1[names(adj.p.s1) %in% "as.factor(RussianGroup)Sanction"][1], adj.p.s1[names(adj.p.s1) %in% "as.factor(RussianGroup)Sanction"][3],
              adj.p.s2[names(adj.p.s2) %in% "as.factor(RussianGroup)Sanction"][1], adj.p.s2[names(adj.p.s2) %in% "as.factor(RussianGroup)Sanction"][3]), 4)     

c3 <- round(c(coef(main1_s1)["as.factor(RussianGroup)Military"], coef(main2_s1)["as.factor(RussianGroup)Military"],
        coef(main1_s2)["as.factor(RussianGroup)Military"], coef(main2_s2)["as.factor(RussianGroup)Military"]), 2)
p3 <- round(c(summary(main1_s1)$coefficients["as.factor(RussianGroup)Military", 4], summary(main2_s1)$coefficients["as.factor(RussianGroup)Military", 4],
              summary(main1_s2)$coefficients["as.factor(RussianGroup)Military", 4], summary(main2_s2)$coefficients["as.factor(RussianGroup)Military", 4]), 4)
a3 <- round(c(adj.p.s1[names(adj.p.s1) %in% "as.factor(RussianGroup)Military"][1], adj.p.s1[names(adj.p.s1) %in% "as.factor(RussianGroup)Military"][3],
              adj.p.s2[names(adj.p.s2) %in% "as.factor(RussianGroup)Military"][1], adj.p.s2[names(adj.p.s2) %in% "as.factor(RussianGroup)Military"][3]), 4)     

means1A <- round(c(mean(experiment1$Military[experiment1$Group == 0], na.rm = TRUE), mean(experiment1$Taiwan[experiment1$Group == 0], na.rm = TRUE),
                   mean(experiment2$Military[experiment2$Group == 0], na.rm = TRUE), mean(experiment2$Taiwan[experiment2$Group == 0], na.rm = TRUE)), 2)
n1A <- c(nrow(experiment1), NA,
         nrow(experiment2), NA)

table.1A <- rbind(c1, p1, a1,
                  c2, p2, a2, 
                  c3, p3, a3,
                  means1A, n1A)
colnames(table.1A) <- c("Experiment 1: Use of Force", "Experiment 1: Use of Force Against Taiwan",
                        "Experiment 2: Use of Force", "Experiment 2: Use of Force Against Taiwan")
rownames(table.1A) <- c("Invasion", "p-value", "adjusted p-value",
                        "Economic Measures", "p-value", "adjusted p-value",
                        "Military Measures", "p-value", "adjusted p-value",
                        "Baseline Support", "N")

# Table 1B
c4 <- round(c(coef(main1_s1_inv)["as.factor(RussianGroup)Sanction"], coef(main2_s1_inv)["as.factor(RussianGroup)Sanction"],
              coef(main1_s2_inv)["as.factor(RussianGroup)Sanction"], coef(main2_s2_inv)["as.factor(RussianGroup)Sanction"]), 2)
p4 <- round(c(summary(main1_s1_inv)$coefficients["as.factor(RussianGroup)Sanction", 4], summary(main2_s1_inv)$coefficients["as.factor(RussianGroup)Sanction", 4],
              summary(main1_s2_inv)$coefficients["as.factor(RussianGroup)Sanction", 4], summary(main2_s2_inv)$coefficients["as.factor(RussianGroup)Sanction", 4]), 4)
a4 <- round(c(adj.p.s1[names(adj.p.s1) %in% "as.factor(RussianGroup)Sanction"][2], adj.p.s1[names(adj.p.s1) %in% "as.factor(RussianGroup)Sanction"][4],
              adj.p.s2[names(adj.p.s1) %in% "as.factor(RussianGroup)Sanction"][2], adj.p.s2[names(adj.p.s1) %in% "as.factor(RussianGroup)Sanction"][4]), 4)     

c5 <- round(c(coef(main1_s1_inv)["as.factor(RussianGroup)Military"], coef(main2_s1_inv)["as.factor(RussianGroup)Military"],
              coef(main1_s2_inv)["as.factor(RussianGroup)Military"], coef(main2_s2_inv)["as.factor(RussianGroup)Military"]), 2)
p5 <- round(c(summary(main1_s1_inv)$coefficients["as.factor(RussianGroup)Military", 4], summary(main2_s1_inv)$coefficients["as.factor(RussianGroup)Military", 4],
              summary(main1_s2_inv)$coefficients["as.factor(RussianGroup)Military", 4], summary(main2_s2_inv)$coefficients["as.factor(RussianGroup)Military", 4]), 4)
a5 <- round(c(adj.p.s1[names(adj.p.s1) %in% "as.factor(RussianGroup)Military"][2], adj.p.s1[names(adj.p.s1) %in% "as.factor(RussianGroup)Military"][4],
              adj.p.s2[names(adj.p.s1) %in% "as.factor(RussianGroup)Military"][2], adj.p.s2[names(adj.p.s1) %in% "as.factor(RussianGroup)Military"][4]), 4)     

means1B <- round(c(mean(experiment1$Military[experiment1$Group == 1], na.rm = TRUE), mean(experiment1$Taiwan[experiment1$Group == 1], na.rm = TRUE),
                   mean(experiment2$Military[experiment2$Group == 1], na.rm = TRUE), mean(experiment2$Taiwan[experiment2$Group == 1], na.rm = TRUE)), 2)
n1B <- c(nrow(data.sub1), NA,
         nrow(data.sub2), NA)

table.1B <- rbind(c4, p4, a4,
                  c5, p5, a5, 
                  means1B, n1B)
colnames(table.1B) <- c("Experiment 1: Use of Force", "Experiment 1: Use of Force Against Taiwan",
                        "Experiment 2: Use of Force", "Experiment 2: Use of Force Against Taiwan")

rownames(table.1B) <- c("Economic Measures", "p-value", "adjusted p-value",
                        "Military Measures", "p-value", "adjusted p-value",
                        "Baseline Support", "N")

table.1A
table.1B

## ------------------------------
## Figure 3
## ------------------------------
pdf(file = "./Figures_Paper/Figure_03.pdf", width = 16, height = 6)
main.result.taiwan(data = experiment2, plot.covs = TRUE, paper = TRUE, title.l = "Figure 3a", title.r = "Figure 3b")
dev.off()

## ------------------------------
## Figure 4
## ------------------------------
m1af <- mediation.multAll(data1 = experiment1, data2 = experiment2, treatment.value = "Invasion", control.value = "Control", outcome = "Military", alt = TRUE, title = "Figure 4a\n")
t1af <- mediation.multAll(data1 = experiment1, data2 = experiment2, treatment.value = "Invasion", control.value = "Control", outcome = "Taiwan", alt = TRUE, title = "Figure 4b\n")

pdf(file = "./Figures_Paper/Figure_04.pdf", width = 12, height = 7)
ggarrange(m1af, t1af, ncol = 2, nrow = 1)
dev.off()

## ------------------------------------------------------------
## In-text numbers not reported in Table 1
## ------------------------------------------------------------

# In the paper we say:

# To provide an intuitive interpretation of the treatment effects, we created 
# two binary outcome variables measuring support for using force in general and 
# against Taiwan in particular: 1 indicating ``somewhat support" or ``strongly support" and 
# 0 otherwise. In Experiment 1, slightly less than half of the control group supported 
# using force in general and against Taiwan. Following exposure to the Russian invasion 
# vignette, the percentage of respondents who support using force in general and against
# Taiwan increased by more than 8 percentage points if compared to those in the control 
# group (adj-$p = 0.002$ and  $0.0018$, respectively). In Experiment 2, around 57\% of 
# the control group supported using force in general and against Taiwan in particular. 
# Compared to Experiment 1, the treatment effect for using force is smaller and cannot 
# be statistically distinguished from zero (a 3.9 percentage points increase, 
# adj-$p = 0.17$) but remains of a similar magnitude for the support for the use 
# of force against Taiwan (an 8.1 percentage points increase, adj-$p = 0.003$).

# The numbers in this paragraph come form the following table:

# Main Regressions:
# Outcome: Support for the Use of Force
main1_s1_b <- lm(I(Military >= 4) ~ as.factor(RussianGroup), data = experiment1)
main1_s2_b <- lm(I(Military >= 4) ~ as.factor(RussianGroup), data = experiment2)

# Outcome: Support for the Use of Force Against Taiwan
main2_s1_b <- lm(I(Taiwan >= 4)~ as.factor(RussianGroup), data = experiment1)
main2_s2_b <- lm(I(Taiwan >= 4)~ as.factor(RussianGroup), data = experiment2)

# Subset the data to exclude the control group
# Invasion Treatment becomes the baseline

# Regressions with Invasion Treatment as baseline:
# Outcome: Support for the Use of Force
main1_s1_inv_b <- lm(I(Military  >= 4) ~ as.factor(RussianGroup), data = data.sub1)
main1_s2_inv_b <- lm(I(Military  >= 4) ~ as.factor(RussianGroup), data = data.sub2)

# Outcome: Support for the Use of Force Against Taiwan
main2_s1_inv_b <- lm(I(Taiwan >= 4) ~ as.factor(RussianGroup), data = data.sub1)
main2_s2_inv_b <- lm(I(Taiwan >= 4) ~ as.factor(RussianGroup), data = data.sub2)

# Regressions for the Taiwan Questions used in Figure 3
# We need them to adjust the p-values 
main3a_s2_b <- lm(I(Taiwan_Invasion >= 4) ~ as.factor(RussianGroup), data = experiment2)
main3b_s2_b <- lm(I(Taiwan_Coersion >= 4) ~ as.factor(RussianGroup), data = experiment2)

adj.p.s1_b <- p.adjust(c(summary(main1_s1_b)$coefficients[2:4,4], summary(main1_s1_inv_b)$coefficients[2:3,4], summary(main2_s1_b)$coefficients[2:4,4], summary(main2_s1_inv_b)$coefficients[2:3,4]), method = "fdr")
adj.p.s2_b <- p.adjust(c(summary(main1_s2_b)$coefficients[2:4,4], summary(main1_s2_inv_b)$coefficients[2:3,4], summary(main2_s2_b)$coefficients[2:4,4], summary(main2_s2_inv_b)$coefficients[2:3,4], summary(main3a_s2_b)$coefficients[2:4,4], summary(main3b_s2_b)$coefficients[2:4,4]), method = "fdr")

# Table: Binary Outcomes
c1 <- round(c(coef(main1_s1_b)["as.factor(RussianGroup)Invasion"], coef(main2_s1_b)["as.factor(RussianGroup)Invasion"],
              coef(main1_s2_b)["as.factor(RussianGroup)Invasion"], coef(main2_s2_b)["as.factor(RussianGroup)Invasion"]), 4)
p1 <- round(c(summary(main1_s1_b)$coefficients["as.factor(RussianGroup)Invasion", 4], summary(main2_s1_b)$coefficients["as.factor(RussianGroup)Invasion", 4],
              summary(main1_s2_b)$coefficients["as.factor(RussianGroup)Invasion", 4], summary(main2_s2_b)$coefficients["as.factor(RussianGroup)Invasion", 4]), 4)
a1 <- round(c(adj.p.s1_b[names(adj.p.s1_b) %in% "as.factor(RussianGroup)Invasion"][1], adj.p.s1_b[names(adj.p.s1_b) %in% "as.factor(RussianGroup)Invasion"][2],
              adj.p.s2_b[names(adj.p.s2_b) %in% "as.factor(RussianGroup)Invasion"][1], adj.p.s2_b[names(adj.p.s2_b) %in% "as.factor(RussianGroup)Invasion"][2]), 4)     

table.Binary <- rbind(c1, p1, a1)
colnames(table.Binary) <- c("Experiment 1: Use of Force", "Experiment 1: Use of Force Against Taiwan",
                        "Experiment 2: Use of Force", "Experiment 2: Use of Force Against Taiwan")
rownames(table.Binary) <- c("Invasion", "p-value", "adjusted p-value")

table.Binary

# Finally, in the paper we say:

# "Next, we examine how the Russian invasion and the subsequent 
# countermeasures influence endorsements of particular ways of 
# using military force against Taiwan in Experiment 2. Consistent 
# with the findings of \cite{liu2023assessing}, we find slightly 
# over half of our respondents express support for both outright 
# invasion (58\%) and military coercion (53\%). Figure \ref{fig:Taiwan} 
# shows that the invasion treatment leads to a modest increase 
# in support for both war and military coercion, with a slightly 
# larger effect on coercion (0.16 unit increase, adj-$p = 0.009$) than 
# war (0.11 unit increase, adj-$p = 0.06$). Moreover, these modest 
# treatment effects on military coercion are persistent even after 
# providing additional information about Western economic (0.12 unit 
# increase, adj-$p = 0.06$) and military countermeasures (0.11 unit 
# increase, adj-$p = 0.06$). Conversely, support for an outright 
# reunification war cannot be statistically distinguished from zero 
# after presenting additional information about Western countermeasures. 
# Thus, military measures appear to be more effective in mitigating the 
# invasion's impact on boosting support for an outright invasion of Taiwan."

# The numbers in this paragraph come form the following table:
c1 <- round(c(coef(main3a_s2)["as.factor(RussianGroup)Invasion"], coef(main3b_s2)["as.factor(RussianGroup)Invasion"]), 4)
p1 <- round(c(summary(main3a_s2)$coefficients["as.factor(RussianGroup)Invasion", 4], summary(main3b_s2)$coefficients["as.factor(RussianGroup)Invasion", 4]), 4)
a1 <- round(c(adj.p.s2[names(adj.p.s2) %in% "as.factor(RussianGroup)Invasion"][3], adj.p.s2[names(adj.p.s2) %in% "as.factor(RussianGroup)Invasion"][4]), 4)     

c2 <- round(c(coef(main3a_s2)["as.factor(RussianGroup)Sanction"], coef(main3b_s2)["as.factor(RussianGroup)Sanction"]), 4)
p2 <- round(c(summary(main3a_s2)$coefficients["as.factor(RussianGroup)Sanction", 4], summary(main3b_s2)$coefficients["as.factor(RussianGroup)Invasion", 4]), 4)
a2 <- round(c(adj.p.s2[names(adj.p.s2) %in% "as.factor(RussianGroup)Sanction"][5], adj.p.s2[names(adj.p.s2) %in% "as.factor(RussianGroup)Sanction"][6]), 4)     

c3 <- round(c(coef(main3a_s2)["as.factor(RussianGroup)Military"], coef(main3b_s2)["as.factor(RussianGroup)Military"]), 4)
p3 <- round(c(summary(main3a_s2)$coefficients["as.factor(RussianGroup)Military", 4], summary(main3b_s2)$coefficients["as.factor(RussianGroup)Military", 4]), 4)
a3 <- round(c(adj.p.s2[names(adj.p.s2) %in% "as.factor(RussianGroup)Military"][5], adj.p.s2[names(adj.p.s2) %in% "as.factor(RussianGroup)Military"][6]), 4)     

table.Taiwan <- rbind(c1, p1, a1,
                      c2, p2, a2,
                      c3, p3, a3)
colnames(table.Taiwan) <- c("Experiment 2: Taiwan Invasion", "Experiment 2: Coercion")
rownames(table.Taiwan) <- c("Invasion", "p-value", "adjusted p-value",
                            "Economic Measures", "p-value", "adjusted p-value",
                            "Military Measures", "p-value", "adjusted p-value")

table.Taiwan

